package com.syt.hmanage.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: swagger3的配置
 * @Date: 2022/11/26 13:47
 * @Description: swagger配置类
 */

@EnableOpenApi
@EnableKnife4j
@Configuration
public class Swagger3Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                //.groupName("hospital")
                .apiInfo(apiInfo())
                .select()
                //.apis(RequestHandlerSelectors.basePackage("com.yeb.server.controller"))
                //否定接口 过滤什么路径(url)
                //.paths(PathSelectors.regex("/admin/.*").negate())
                .paths(PathSelectors.regex("/error.*").negate())
                .build()
                .securitySchemes(securitySchemes())
                .securityContexts(securityContexts());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("尚医通管理系统")
                .description("尚医通接口管理系统文档")
                .contact(new Contact("xxxxx", "", ""))
                //生成文档界面地址
                //.termsOfServiceUrl("http://localhost:81/doc.html")
                .version("v1.0")
                .build();
    }

    private List<SecurityScheme> securitySchemes(){
        //设置请求头信息 参数：api key 名字 { 准备的 ey 名字，value 请求头 }
        List<SecurityScheme> apiKeyList = new ArrayList<>();
        apiKeyList.add(new ApiKey("Authorization", "Authorization", "header"));
        return apiKeyList;
    }

    /*
     *  设置swagger 全局token 并且携带
     * */
    private List<SecurityContext> securityContexts(){
        //需要认证的路径
        List<SecurityContext> securityContextList=new ArrayList<>();
        securityContextList.add(getSecurityContextByPath("/hello/.*"));
        return securityContextList;
    }

    private SecurityContext getSecurityContextByPath(String pathRegex) {
        return SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex(pathRegex))
                .build();
    }

    private List<SecurityReference> defaultAuth() {
        List<SecurityReference> list = new ArrayList<>();
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        list.add(new SecurityReference("Authorization", authorizationScopes));
        return list;

    }
}
